package com.dykj.fireFighting.web.xfxg;

import java.util.*;

import com.dykj.fireFighting.api.socialUnit.xfxg.dto.FireGroupDto;
import com.dykj.fireFighting.api.socialUnit.xfxg.dto.FireGroupSaveDto;
import com.dykj.fireFighting.api.socialUnit.xfxg.entity.FireGroup;
import com.dykj.fireFighting.api.socialUnit.xfxg.service.IFireGroupDeviceService;
import com.dykj.fireFighting.api.socialUnit.xfxg.service.IFireGroupService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


import javax.validation.Valid;



/**
 * 消防巡更-消防群组接口
 *
 * @Author JacksonTu
 * @Date  2021-06-28 14:54:24
 */
@Slf4j
@Api(value = "消防巡更-消防群组接口", tags = {"消防巡更-消防群组接口"})
@RestController
@RequestMapping("/fireGroup")
public class FireGroupController {

    @DubboReference
    private IFireGroupService fireGroupService;

    @DubboReference
    private IFireGroupDeviceService fireGroupDeviceService;
    /**
     * 获取单位id
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 消防巡更-消防群组列表
     *
     * @return
     */
    @AutoLog(value = "消防巡更-消防群组-分页列表查询")
    @ApiOperation(value = "消防巡更-消防群组分页列表", notes = "消防巡更-消防群组分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<FireGroup>> treeGrid(FireGroupDto commonDto) {
        commonDto.setUnitId(getEnterpriseId());
        PageVo<FireGroup> page = fireGroupService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取消防巡更-消防群组信息
     */
    @AutoLog(value = "消防巡更-消防群组-获取消防巡更-消防群组信息")
    @ApiOperation(value = "获取消防巡更-消防群组信息", notes = "获取消防巡更-消防群组信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<FireGroupSaveDto> info(@PathVariable("id") String id){
        FireGroup fireGroup = fireGroupService.getById(id);
        List<String> device=fireGroupDeviceService.getDeviceByFireGroupId(id,0);
        List<String> deviceTradition=fireGroupDeviceService.getDeviceByFireGroupId(id,1);
        FireGroupSaveDto fireGroupSaveDto=new FireGroupSaveDto();
        fireGroupSaveDto.setCycle(fireGroup.getCycle());
        fireGroupSaveDto.setName(fireGroup.getName());
        fireGroupSaveDto.setCreateBy(fireGroup.getCreateBy());
        fireGroupSaveDto.setCreateTime(fireGroup.getCreateTime());
        fireGroupSaveDto.setRemark(fireGroup.getRemark());
        fireGroupSaveDto.setDevice(device);
        fireGroupSaveDto.setDeviceTradition(deviceTradition);
        return CommonResult.success(fireGroupSaveDto);
    }

    /**
     * 保存消防巡更-消防群组信息
     */
    @AutoLog(value = "消防巡更-消防群组-保存消防巡更-消防群组信息")
    @ApiOperation(value = "保存消防巡更-消防群组信息", notes = "保存消防巡更-消防群组信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody FireGroupSaveDto fireGroup) {
        try {
            fireGroup.setUnitId(getEnterpriseId());
            fireGroup.setCreateBy(getName());
            fireGroupService.insertFireGroup(fireGroup);

            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改消防巡更-消防群组信息
     */
    @AutoLog(value = "消防巡更-消防群组-修改消防巡更-消防群组信息")
    @ApiOperation(value = "修改消防巡更-消防群组信息", notes = "修改消防巡更-消防群组信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody FireGroupSaveDto fireGroup) {
        try {
            fireGroup.setUpdateBy(getName());
            fireGroupService.updateFireGroup(fireGroup);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除消防巡更-消防群组信息
     */
    @AutoLog(value = "消防巡更-消防群组-删除消防巡更-消防群组信息")
    @ApiOperation(value = "删除消防巡更-消防群组信息", notes = "删除消防巡更-消防群组信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                fireGroupService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
